package test2.controller;

import com.perye.dokit.aop.log.Log;
import test2.entity.Test2;
import test2.service.Test2Service;
import test2.query.Test2QueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Api(tags = "testCpntroller管理")
@RestController
@RequestMapping("/api/test2")
public class Test2Controller {

    private final Test2Service test2Service;

    public Test2Controller(Test2Service test2Service) {
        this.test2Service = test2Service;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@dokit.check('test2:list')")
    public void download(HttpServletResponse response, Test2QueryCriteria criteria) throws IOException {
        test2Service.download(test2Service.queryAll(criteria), response);
    }

    @Log("查询testCpntroller")
    @ApiOperation("查询testCpntroller")
    @GetMapping()
    @PreAuthorize("@dokit.check('test2:list')")
    public ResponseEntity<Object> getTest2s(Test2QueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(test2Service.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增testCpntroller")
    @ApiOperation("新增testCpntroller")
    @PostMapping
    @PreAuthorize("@dokit.check('test2:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Test2 resources){
        return new ResponseEntity<>(test2Service.create(resources),HttpStatus.CREATED);
    }

    @Log("修改testCpntroller")
    @ApiOperation("修改testCpntroller")
    @PutMapping
    @PreAuthorize("@dokit.check('test2:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Test2 resources){
    test2Service.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除testCpntroller")
    @ApiOperation("删除testCpntroller")
    @PreAuthorize("@dokit.check('test2:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
    test2Service.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
